package com.astn.bks;

import java.util.ArrayList;
import java.util.Currency;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.astn.adapter.booking.DestinationAdapter;
import com.astn.adapter.booking.ProviderAdapter;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.WebserviceUtils;
import com.astn.entity.booking.QuoteTown;
import com.astn.entity.booking.QuoteUser;

public class CTopDestination extends Activity {

	// private ViewFlipper homeFlipper;
	private ListView lsvTop;
	private Button btnNext;
	private Button btnBack;
	private TextView txvPage;
	private ArrayList<QuoteTown> lstTown;// Top destination
	private int topDestinationCurrentPage = 0;
	private int totalDestinationPage = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ctopdestination);
		lsvTop = (ListView) findViewById(R.id.lsvTop);
		btnNext = (Button) findViewById(R.id.btnNextPage);
		btnBack = (Button) findViewById(R.id.btnPreviousPage);
		txvPage = (TextView) findViewById(R.id.txvPage);
		getNumberPageDestination();
		txvPage.setText(String.valueOf(topDestinationCurrentPage + 1) + "/"
				+ String.valueOf(totalDestinationPage));
		reloadTopDestination();
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getNumberPageDestination();
				if (topDestinationCurrentPage < totalDestinationPage - 1) {
					topDestinationCurrentPage++;
				} else {
					topDestinationCurrentPage = 0;
				}
				txvPage.setText(String.valueOf(topDestinationCurrentPage + 1)
						+ "/" + String.valueOf(totalDestinationPage));
				reloadTopDestination();
			}

		});
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				getNumberPageDestination();
				if (topDestinationCurrentPage > 0) {
					topDestinationCurrentPage--;
				} else {
					topDestinationCurrentPage = totalDestinationPage - 1;
				}
				txvPage.setText(String.valueOf(topDestinationCurrentPage + 1)
						+ "/" + String.valueOf(totalDestinationPage));
				reloadTopDestination();

			}

		});

	}

	private void getNumberPageDestination() {
		totalDestinationPage = 2;
	}

	private void reloadTopDestination() {
		lstTown = new ArrayList<QuoteTown>();
		Object resultObj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGE", topDestinationCurrentPage);
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			resultObj = WebserviceUtils.call("com_xmlrpc", "getTopDestination",
					mapPara);
		} catch (Exception e) {

		}
		if (resultObj != null) {
			JSONArray arr = null;
			try {
				arr = (JSONArray) JSONValue.parse((String) resultObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (arr != null) {
				for (int i = 0; i < arr.size(); i++) {
					JSONObject obj = (JSONObject) arr.get(i);
					try {
						QuoteTown tmpTown = new QuoteTown();
						tmpTown.setCitycode((String) obj.get("citycode"));
						tmpTown.setCityname((String) obj.get("cityname"));
						tmpTown.setCitynumber((String) obj.get("citynumber"));
						tmpTown.setCountryid(Integer.parseInt((String) obj
								.get("countryid")));
						tmpTown.setId(Integer.parseInt((String) obj.get("id")));
						tmpTown.setRegionid(Integer.parseInt((String) obj
								.get("regionid")));
						tmpTown.setStateid(Integer.parseInt((String) obj
								.get("stateid")));
						lstTown.add(tmpTown);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		}

		DestinationAdapter adap = new DestinationAdapter(
				getApplicationContext(), lstTown);
		lsvTop.setAdapter(adap);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// switch (event.getAction()) {
	// case MotionEvent.ACTION_DOWN:
	// touchDown = event.getX();
	// break;
	// case MotionEvent.ACTION_UP:
	// touchUp = event.getX();
	// if (touchUp < touchDown) {
	// homeFlipper.setAnimation(AnimationUtils.loadAnimation(
	// Home.this, android.R.anim.slide_out_right));
	//
	// homeFlipper.showPrevious();
	// }
	// if (touchUp > touchDown) {
	// homeFlipper.setAnimation(AnimationUtils.loadAnimation(
	// Home.this, android.R.anim.slide_in_left));
	// homeFlipper.showNext();
	// }
	// break;
	//
	// default:
	// break;
	// }
	// return true;
	// }

}
